package com.lzy.cloud.consumer.controller;

import com.lzy.framework.core.output.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ellengou
 * Date: 2018-03-16
 * Time: PM 4:02
 */
@RestController
public class DcController {

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer")
    public Result dc() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("lzy-cloud-producer");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
        System.out.println(url);
        return new Result(url);
//        return restTemplate.getForObject(url, String.class);
    }

    @RequestMapping(value = "/api",method = {RequestMethod.PUT})
    public Result api() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("lzy-cloud-producer");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
        System.out.println(url);
        return new Result(url);
//        return restTemplate.getForObject(url, String.class);
    }
}
